package com.octant.testipc;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import com.tuya.smart.android.user.api.IRegisterCallback;
import com.tuya.smart.android.user.api.IResetPasswordCallback;
import com.tuya.smart.android.user.api.IWhiteListCallback;
import com.tuya.smart.android.user.bean.User;
import com.tuya.smart.android.user.bean.WhiteList;
import com.tuya.smart.home.sdk.TuyaHomeSdk;
import com.tuya.smart.sdk.api.IResultCallback;

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import androidx.appcompat.app.AppCompatActivity;

public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {

    private Spinner spn_country_code;
    private EditText edt_account,edt_password,edt_verify_code;
    private Button btn_send_verify_code,btn_register;
    private ArrayList<String> codeList = new ArrayList<>();
    private Context mContext;

    private final String check = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
    private final Pattern regex = Pattern.compile(check);

    private final int VERIFY_REGISTER_TYPE = 1;
//    private final int VERIFY_LOGIN_TYPE = 2;
//    private final int VERIFY_RESET_TYPE = 3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        mContext = RegisterActivity.this;
        initViews();
    }

    private void initViews() {
        spn_country_code = findViewById(R.id.spn_country_code);
        edt_account = findViewById(R.id.edt_account);
        edt_password = findViewById(R.id.edt_password);
        edt_verify_code = findViewById(R.id.edt_verify_code);
        btn_send_verify_code = findViewById(R.id.btn_send_verify_code);
        btn_register = findViewById(R.id.btn_register);
        TuyaHomeSdk.getUserInstance().getWhiteListWhoCanSendMobileCodeSuccess(new IWhiteListCallback() {
            @Override
            public void onSuccess(WhiteList whiteList) {
                //Toast.makeText(getApplicationContext(), "Whitelist Success:" + whiteList.getCountryCodes(), Toast.LENGTH_SHORT).show();
                String codes = whiteList.getCountryCodes();
                String[] codeArr = codes.split(",");
                codeList.clear();
                for (int i=0; i<codeArr.length; i++){
                    codeList.add(codeArr[i]);
                }
                ArrayAdapter adapter = new ArrayAdapter<String>(mContext,R.layout.support_simple_spinner_dropdown_item,  codeList);
                spn_country_code.setAdapter(adapter);
            }

            @Override
            public void onError(String code, String error) {
                Toast.makeText(getApplicationContext(), "code: " + code + "error:" + error, Toast.LENGTH_SHORT).show();
            }
        });
        btn_send_verify_code.setOnClickListener(this);
        btn_register.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_send_verify_code:{
                String country_code = (String) spn_country_code.getSelectedItem();
                String account = edt_account.getText().toString();
                if (country_code == null || country_code.isEmpty()){
                    Toast.makeText(mContext, "国家码不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (account == null || account.isEmpty()){
                    Toast.makeText(mContext, "邮箱或手机号不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                btn_send_verify_code.setText("已发送验证码");
                btn_send_verify_code.setEnabled(false);
                IResultCallback callback = new IResultCallback() {
                    @Override
                    public void onError(String code, String error) {
                        Toast.makeText(mContext,
                                "code: " + code + "error:" + error,
                                Toast.LENGTH_SHORT).show();
                        btn_send_verify_code.setText("发送验证码");
                        btn_send_verify_code.setEnabled(true);
                    }

                    @Override
                    public void onSuccess() {
                        new Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                btn_send_verify_code.setText("发送验证码");
                                btn_send_verify_code.setEnabled(true);
                            }
                        },60000);
                    }
                };
                TuyaHomeSdk.getUserInstance().sendVerifyCodeWithUserName(account, "", country_code, VERIFY_REGISTER_TYPE, callback);
            }break;
            case R.id.btn_register:{
                String country_code = (String) spn_country_code.getSelectedItem();
                String account = edt_account.getText().toString();
                String password = edt_password.getText().toString();
                String verify_code = edt_verify_code.getText().toString();
                if (country_code == null || country_code.isEmpty()){
                    Toast.makeText(mContext, "国家码不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (account == null || account.isEmpty()){
                    Toast.makeText(mContext, "邮箱或手机号不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (password == null || password.isEmpty()){
                    Toast.makeText(mContext, "密码不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (verify_code == null || verify_code.isEmpty()){
                    Toast.makeText(mContext, "验证码不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                IRegisterCallback callback = new IRegisterCallback() {
                    @Override
                    public void onSuccess(User user) {
                        Toast.makeText(mContext,
                                "Register success",
                                Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onError(String code, String error) {
                        Toast.makeText(mContext,
                                "code: " + code + "error:" + error,
                                Toast.LENGTH_SHORT).show();
                    }
                };
                Matcher matcher = regex.matcher(account);
                boolean isEmail = matcher.matches();
                if (isEmail){
                    TuyaHomeSdk.getUserInstance().registerAccountWithEmail(
                            country_code,
                            account,
                            password,
                            verify_code,
                            callback
                    );
                }else {
                    TuyaHomeSdk.getUserInstance().registerAccountWithPhone(
                            country_code,
                            account,
                            password,
                            verify_code,
                            callback
                    );
                }
            }break;
        }
    }
}
